package com.cwd.arm.application.service.read;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cwd.arm.application.dto.user.UserManageDto;
import com.cwd.arm.application.service.read.facade.AuthReadService;
import com.cwd.arm.application.service.read.facade.UserReadService;
import com.cwd.arm.common.dto.session.UserDto;
import com.cwd.arm.common.encoder.ArmPasswordEncoder;
import com.cwd.arm.infrastructure.dao.RoleDao;
import com.cwd.arm.infrastructure.dao.UserDao;
import com.cwd.arm.infrastructure.po.RolePo;
import com.cwd.arm.infrastructure.po.UserPo;
import com.cwd.arm.infrastructure.query.RoleDaoQuery;
import com.cwd.arm.infrastructure.query.UserDaoQuery;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by yan on 21-11-14.
 */
@Repository
@Primary
public class UserReadServiceImpl implements UserReadService, UserDetailsService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private AuthReadService authReadService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserPo userPo = userDao.getByUsername(username);
        if (userPo == null) {
            userPo = userDao.getByPhone(username);
        }
        if (userPo == null) {
            throw new UsernameNotFoundException("用户不存在");
        }
        ArmPasswordEncoder armPasswordEncoder = ArmPasswordEncoder.getInstance();
        armPasswordEncoder.setMask(userPo.getMask());
        UserDto userDto = convertToDto(userPo);
        setAuthToUser(userDto);
        return userDto;
    }

    @Override
    public UserDto getUserById(Long id) {
        UserDto userDto = convertToDto(userDao.getById(id));
        setAuthToUser(userDto);
        return userDto;
    }

    @Override
    public Page<UserManageDto> selectPage(UserDaoQuery query) {
        Page<UserPo> poPage = userDao.selectPage(query);
        Page<UserManageDto> dtoPage = new Page<>();
        BeanUtils.copyProperties(poPage, dtoPage);
        List<UserManageDto> dtoList = poPage.getRecords().stream()
                .map(po -> convertToUserManageDto(po)).collect(Collectors.toList());
        dtoPage.setRecords(dtoList);
        return dtoPage;
    }

    @Override
    public List<UserDto> selectList(UserDaoQuery query) {
        List<UserPo> poList = userDao.selectList(query);
        return convertToDtoList(poList);
    }

    private List<UserDto> convertToDtoList(List<UserPo> poList) {
        return poList.stream().map(po->convertToDto(po)).collect(Collectors.toList());
    }

    private UserDto convertToDto(UserPo po) {
        UserDto dto = new UserDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

    private void setAuthToUser(UserDto dto){
        dto.setAuthDtoList(authReadService.selectByUserId(dto.getId()));
    }

    private UserManageDto convertToUserManageDto(UserPo po) {
        UserManageDto dto = new UserManageDto();
        BeanUtils.copyProperties(po, dto);
        return dto;
    }

}
